package cn.iocoder.yudao.module.member.eid.controller.app;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.api.member.eid.vo.EidVO;
import cn.iocoder.yudao.module.member.eid.dal.dataobject.MemberEidDO;
import cn.iocoder.yudao.module.member.eid.service.MemberEidService;
import cn.iocoder.yudao.module.member.eid.vo.AppEidSaveReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 App - 实名信息")
@RestController
@RequestMapping("/member/idcard")
@Validated
public class AppMemberEidController {

    @Resource
    private MemberEidService memberEidService;

    @PostMapping("/save")
    @Operation(summary = "保存实名信息")
    @ApiAccessLog(sanitizeKeys = {"idcardNo", "idcardFrontUrl", "idcardBackUrl"})
    public CommonResult<Integer> saveIdcard(@Valid @RequestBody AppEidSaveReqVO reqVO) {
        return success(memberEidService.saveIdcard(getLoginUserId(), reqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "获得我的实名信息")
    public CommonResult<EidVO> getIdcard() {
        MemberEidDO entity = memberEidService.getIdcardByUserId(getLoginUserId());
        if (entity == null) {
            EidVO vo = new EidVO();
            vo.setId(0L);
            vo.setUserId(0L);
            return success(vo);
        }
        return success(BeanUtils.toBean(entity, EidVO.class));
    }

}
